package com.example.facecat;

import com.example.facecat.product.ui.MainFrame;
import com.example.facecat.system.*;
import facecat.topin.core.*;
import facecat.topin.sock.*;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.global.resource.Resource;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MainAbility extends Ability {
    public static MainAbility m_mainAbility;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        m_mainAbility = this;
        FCClientSocket.noThread = false;
        FCUIView view = new FCUIView(getContext());
        view.setAllowScroll(false);
        ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        view.setLayoutConfig(config);
        super.setUIContent(view);
        view.onLoad();
        MainFrame mainFrame = new MainFrame();
        mainFrame.setNative(view.getNative());
        mainFrame.loadXml(getXMlString("mainframe"),  null);
        mainFrame.getNative().update();
        mainFrame.getNative().invalidate();
        //super.setMainRoute(MainAbilitySlice.class.getName());
    }

    public String getXMlString(String name){
        String path = "resources/rawfile/" + name + ".xml";
        try {
            Resource resource = this.getResourceManager().getRawFileEntry(path).openRawFile();
            InputStreamReader inputStreamReader = new InputStreamReader(resource, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String lineTxt = "";
            StringBuilder sb = new StringBuilder();
            while((lineTxt = bufferedReader.readLine()) != null){
                sb.append(lineTxt);
            }
            return sb.toString();
        }catch (Exception ex){

        }
        return "";
    }
}
